#!/bin/sh
#
# updateTNSNames.sh: shell script to update tnsnames.ora 
#
# usage: updateTNSNames.sh <New database sid name> <Oracle Home path> 
set +x
nsid=$1
oracleHome=$2
hostip=$3

if [ "$notnsupdate" = "true" ]; then
   echo "Do not update tnsNames.ora has been set, no-op"
   exit 0
fi

if [ -z "$nsid" ]; then
   exit 1;
fi

if [ -z "$oracleHome" ]; then
   exit 1;
fi  

tnsadmin="$oracleHome/network/admin"
port="1521"

if [ ! -z "$tnsadmindir" ]; then
   tnsadmin=$tnsadmindir
fi

if [ ! -z "$tnsip" ]; then
   hostip=$tnsip
fi

if [ ! -z "$tnsport" ]; then
   port=$tnsport
fi
nsid1=$nsid"1"

svcname=$nsid
if [ ! -z "$tnsdomain" ]; then
   svcname=$nsid"."$tnsdomain
fi

tnsentry="$nsid = (DESCRIPTION =
"\
"    (ADDRESS = (PROTOCOL = TCP)(HOST = $hostip)(PORT = $port))
"\
"    (CONNECT_DATA =
"\
"      (SERVER = DEDICATED)
"\
"      (SERVICE_NAME = $svcname)
"\
"    )
"\
"  )"

tnsentry1="$nsid1 = (DESCRIPTION =
"\
"    (ADDRESS = (PROTOCOL = TCP)(HOST = $hostip)(PORT = $port))
"\
"    (CONNECT_DATA =
"\
"      (SERVER = DEDICATED)
"\
"      (INSTANCE_NAME = $nsid1)
"\
"      (SERVICE_NAME = $svcname)
"\
"    )
"\
"  )"

tnsnameora="$tnsadmin/tnsnames.ora"

if [ ! -f $tnsnameora ]; then
   echo "$tnsnameora does not exists"
fi 

echo "$tnsentry" >> $tnsnameora

if [ -z "$standalone" ] || [ "$standalone" != "true" ]; then
   if [ ! -z "$israc" ] && [ "$israc" = "true" ]; then
      echo "$tnsentry1" >> $tnsnameora
   fi
fi

exit 0

#<alias> = (DESCRIPTION =
#    (ADDRESS = (PROTOCOL = TCP)(HOST = <ip>)(PORT = <port>))
#    (CONNECT_DATA =
#      (SERVER = DEDICATED)
#      (SERVICE_NAME = <dbname>)
#    )
#  )
